package init

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gowork_0911/gym/basic/config"
	"gowork_0911/gym/handler/model"
)

func MysqlInit() {
	var err error
	mysqlIns := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlIns.User,
		mysqlIns.Password,
		mysqlIns.Host,
		mysqlIns.Port,
		mysqlIns.Database,
	)
	config.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}
	err = config.DB.AutoMigrate(&model.User{}, &model.Friend{}, &model.Course{})
	if err != nil {
		fmt.Println("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}
}
